﻿/* Copyright (c) 2010 EPiServer AB
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
 * documentation files (the "Software"), to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and
 * to permit persons to whom the Software is furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO
 * THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
using System;
using System.Web.UI;
using EPiServer.Web.Hosting;
using EPiServer.Web.WebControls;

namespace EPiServer.Municipality.Web.UI.WebControls {
    public class FileSystemListData : Control, INamingContainer {
        /* *******************************************************************
         *  Properties 
         * *******************************************************************/
        #region private FileSystemItem DataItem
        /// <summary>
        /// Gets the DataItem of the FileSystemListData
        /// </summary>
        /// <value></value>
        private FileSystemItem DataItem {
            get {
                return _dataItem;
            }
        }
        private FileSystemItem _dataItem;
        #endregion
        #region public int DataItemIndex
        /// <summary>
        /// Gets the DataItemIndex of the FileSystemListData
        /// </summary>
        /// <value></value>
        public int DataItemIndex {
            get {
                return _dataItemIndex;
            }
        }
        private int _dataItemIndex;
        #endregion
        #region public int DisplayIndex
        /// <summary>
        /// Gets the DisplayIndex of the FileSystemListData
        /// </summary>
        /// <value></value>
        public int DisplayIndex {
            get {
                return _displayIndex;
            }
        }
        private int _displayIndex;
        #endregion
        #region public string Extension
        /// <summary>
        /// Gets the Extension of the FileSystemListData
        /// </summary>
        /// <value></value>
        public string Extension {
            get {
                if (DataItem.IsFile) {
                    var file = DataItem.Item as UnifiedFile;
                    if (file != null)
                        return String.Format(file.Extension.TrimStart(new char[] { '.' }));
                } else if (DataItem.IsDirectory) {
                    return "dir";
                }
                return String.Empty;
            }
        }
        #endregion
        #region public string Description
        /// <summary>
        /// Gets the Description of the FileSystemListData
        /// </summary>
        /// <value></value>
        public string Description {
            get {
                if (DataItem.IsFile) {
                    UnifiedFile file = (UnifiedFile)DataItem.Item;
                    if (!string.IsNullOrEmpty(file.Summary.Dictionary["Description"] as string))
                        return (string)file.Summary.Dictionary["Description"];
                    if (!string.IsNullOrEmpty(file.Summary.Subject))
                        return file.Summary.Subject;
                }
                return String.Empty;
            }
        }
        #endregion
        #region public string Title
        /// <summary>
        /// Gets the Title of the FileSystemListData
        /// </summary>
        /// <value></value>
        public string Title {
            get {
                if (DataItem.IsFile) {
                    UnifiedFile file = (UnifiedFile)DataItem.Item;
                    if (!string.IsNullOrEmpty((string)file.Summary.Dictionary["Title"]))
                        return (string)file.Summary.Dictionary["Title"];
                    if (!string.IsNullOrEmpty(file.Summary.Title))
                        return file.Summary.Title;
                    return file.Name;
                }
                if (DataItem.IsDirectory) {
                    return ((UnifiedDirectory)DataItem.Item).Name;
                }
                return String.Empty;
            }
        }
        #endregion
        #region public string Path
        /// <summary>
        /// Gets the Path of the FileSystemListData
        /// </summary>
        /// <value></value>
        public string Path {
            get {
                return DataItem.Path;
            }
        }
        #endregion
        #region public long FileSize
        /// <summary>
        /// Gets the FileSize of the FileSystemListData
        /// </summary>
        /// <value></value>
        public long FileSize {
            get {
                return DataItem.Length;
            }
        }
        #endregion
        #region public string Name
        /// <summary>
        /// Gets the Name of the FileSystemListData
        /// </summary>
        /// <value></value>
        public string Name {
            get {
                return DataItem.Name;
            }
        }
        #endregion
        /* *******************************************************************
         *  Constructors 
         * *******************************************************************/
        #region public FileSystemListData(object dataItem, int index)
        /// <summary>
        /// Initializes a new instance of the <b>FileSystemListData</b> class.
        /// </summary>
        /// <param name="dataItem"></param>
        /// <param name="index"></param>
        public FileSystemListData(FileSystemItem dataItem, int index) {
            if (dataItem == null)
                throw new ArgumentNullException("dataItem");
            _dataItem = dataItem;
            _dataItemIndex = index;
        }
        #endregion
        /* *******************************************************************
         *  Methods 
         * *******************************************************************/

        /* *******************************************************************
         *  Event methods 
         * *******************************************************************/

    }
}